import { IState, QUI_Button, QUI_Container, QUI_Group, QUI_Image, Texture, TextureFormat, tt } from "../common.js";
import { MainWin } from "../mainwin.js";
import { GameResource } from "../resource/gameresource.js";
import { State_Main } from "./state_main.js";

export class State_MainMenu implements IState<MainWin> {
    container: QUI_Container;
    main: MainWin;



    fadeto: number = 0;
    OnInit(mgr: MainWin): void {
        this.main = mgr;
        this.container = new QUI_Container();
        mgr.canvas.InsertChild(this.container);//插在前面

        this.OnInitAsync();
        this.timer = 0;

    }
    async OnInitAsync() {
        let gametxt = await tt.loader.LoadStringAsync("gamedata/game.json");

        this.main.config = JSON.parse(gametxt);
        let logo = await GameResource.LoadTexture(this.main.config.gamedata.logo);
        let map = await GameResource.LoadMap(this.main.config.playerdata.position.map);

        {//设置背景图
            let img = new QUI_Image();
            img.localRect.SetAsFill();
            img.SetByTexture(logo);
            img.localRect.radioX1 = 0.25;
            img.localRect.radioX2 = 0.75;
            img.localRect.radioY1 = 0.15;
            img.localRect.radioY2 = 0.5;
            img.keepAspect = true;
            this.container.AddChild(img);
        }
        {
            let btn = new QUI_Button();
            btn.SetText('新的游戏');

            btn.localRect.setHPosByCenter(100);
            btn.localRect.setVPosByCenter(32);
            btn.localRect.offsetY1 = 16;
            btn.localRect.offsetY2 = 40;
            this.container.AddChild(btn);
            btn.OnClick = () => {
                if (this.fadeto == 0) {
                    this.fadeto = 1;
                }
            }
        }
        {
            let btn = new QUI_Button();
            btn.SetText('加载存档');

            btn.localRect.setHPosByCenter(100);
            btn.localRect.setVPosByCenter(32);
            btn.localRect.offsetY1 = 50;
            btn.localRect.offsetY2 = 74;
            this.container.AddChild(btn);
        }


        this.container.localColor.A = 0;
        this.container.OnUpdate(this.main.canvas, 0);
    }
    timer: number = 0;
    OnUpdate(delta: number): void {
        if (this.fadeto == 0) {
            this.timer += delta;
            if (this.timer > 1)
                this.timer = 1;
        }
        if (this.fadeto == 1) {
            this.timer -= delta;
            if (this.timer < 0) {
                this.main.ChangeState(new State_Main());
            }
        }
        let alpha = 0;
        if (this.timer < 1)
            alpha = this.timer;
        else
            alpha = 1;

        this.container.localColor.A = alpha;
    }
    OnExit(): void {
        this.main.canvas.RemoveChild(this.container);
    }
    OnResize(width: number, height: number): void {

    }
    OnKey(keycode: string, press: boolean): void {

    }
    OnPointAfterGUI(id: number, x: number, y: number, press: boolean, move: boolean): void {

    }
    OnWheelAfterGUI(dx: number, dy: number, dz: number): void {

    }

}